<?php
require_once('application/libraries/user.php');
class Test extends CI_Controller{
    function __construct(){
    	parent::__construct();
    }
    function index(){
    	$test = "testresultat: ";
		$test .= $this->testLogin();
		$test .= $this->testRegistration();
		$test .= $this->testShowHandler();
		$test .= $this->testPersonHandler();
		$data['title'] = "Projekt";
    	$data['content'] = $test;
    	$data['main_content'] = 'empty';
    	$this->load->view('includes/template', $data);
    }

    function testPersonHandler(){
    
    	$this->load->model('showHandler');
    	$this->load->model('showDAL');
    	
    	$params = array(
    		ID => NULL,
    		FIRSTNAME => 'anna',
    		LASTNAME => 'persson',
    		DESCRIPTION => 'en bra person'
    	);
    	
    	$this->load->library('person', $params);
    	// 1st test, add person
    	if($this->showHandler->addPerson($this->person) == FALSE){
    		echo "1st personhandlertest failed";
    	}
		// 2nd test, check if person exists
		if($this->showDAL->personExists($this->person) == FALSE){
			echo "2nd personhandlertest failed";
		}
		// 3d test, delete show
		if($this->showHandler->deletePerson($this->person) == FALSE){
    		echo "3d personhandlertest failed";
    	}
    	
    	// 4th test, check if show is deleted
    	if($this->showDAL->personExists($this->person) == TRUE){
    		echo "4th personhandlertest failed";
    	}

    }
    function testShowHandler(){
    	$this->load->model('showHandler');
    	$this->load->model('showDAL');
    	$params = array(
    		ID => NULL,
    		NAME => 'testshow',
    		START_YEAR => '2009',
    		END_YEAR => '2011',
    		DESCRIPTION => 'en bra show'
    	);
    	$this->load->library('show', $params);
    	// 1st test, add show
    	if($this->showHandler->addShow($this->show) == FALSE){
    		echo "1st showhandlertest failed";
    	}
    	
    	// 2nd test, check if show exists
    	if($this->showDAL->showExists($this->show) == FALSE){
    		echo "2nd showhandlertest failed";
    	}
    	
    	// 3d test, delete show
    	if($this->showHandler->deleteShow($this->show) == FALSE){
    		echo "3d showhandlertest failed";
    	}
    	
    	// 4th test, check if show is deleted
    	if($this->showDAL->showExists($this->show) == TRUE){
    		echo "4th showhandlertest failed";
    	}
    }
    function testLogin(){
    	$this->load->model('loginHandler');
    	// 1st test, logout
    	$this->loginHandler->doLogout();
    	if($this->loginHandler->isLoggedIn() == TRUE){
    		echo "First logintest failed";
    	}
    	// 2nd test, fail to log in
    	if($this->loginHandler->doLogin('anna', 'wrongpassword') == TRUE){
    		echo "Second logintest failed";
    	}
    	// 3d test, login successfull
    	if($this->loginHandler->doLogin('testuser', 'testpass') == FALSE){
    		echo "Third logintest failed";
    	}
    	// 4th test check if user is logged in
    	if($this->loginHandler->isLoggedIn() == FALSE){
    		echo "4th logintest failed";
    	}
    	// 5th test, logout
    	$this->loginHandler->doLogout();
    	if($this->loginHandler->isLoggedIn() == TRUE){
    		echo "5th logintest failed";
    	}
   	}
 
    function testRegistration(){
		$params = array(
			'id' => NULL, 
			'username' => 'julle87',
			'firstname' => 'julia',
			'lastname' => 'svensson',
			'password' => 'juliasv@email.se',
			'email' => 'losen'
		);
    	$user = new User($params);
    	// Successful registration
    	$this->load->model('registrationHandler');	
	  	if($this->registrationHandler->doRegistration($user) == FALSE){
	  	echo "First registrationtest failed";
	  }
    	// Check if user actually exists
    	if($this->userDAL->usernameExists($user) == FALSE){
    		echo "Second registrationtest failed";
    	}
    	// Already existing username
    	if($this->registrationHandler->doRegistration($user) == TRUE){
	  	echo "Third registrationtest failed";
	  }
	  // Delete user
	  if($this->registrationHandler->deleteUser($user->getUsername()) == FALSE){
	  	echo "4th registrationtest failed";
	  }
	  // Check if user is deleted
	  if($this->userDAL->usernameExists($user)){
	  	echo "5th registrationtest failed";
	  }
    }
    
    function testCookies(){
    	// TODO: Cookietest
    }
}
